﻿using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace IchiaTester
{
    public partial class VisualizerForm : Form
    {
        private MainForm mainform;
        public VisualizerForm(MainForm main)
        {
            InitializeComponent();
            mainform = main;
        }

        /// <summary>
        /// 窗口加载前执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VisualizerForm_Load(object sender, EventArgs e)
        {
            // 取得System.Drawing.Color的所有Public Static Property
            PropertyInfo[] properties = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);
            foreach (PropertyInfo property in properties)
            {
                // 確保Property的Type是Color
                if (property.PropertyType == typeof(Color))
                {
                    // 取得颜色名稱並打印
                    Color color = (Color)property.GetValue(null, null);
                    cbWaveColor.Items.Add(property.Name);
                    cbUpLimitColor.Items.Add(property.Name);
                    cbLowLimitColor.Items.Add(property.Name);
                }
            }
            tbTitle.Text = string.Empty;
            tbFrameHead.Text = string.Empty;
            tbLowLimit.Text = string.Empty;
            tbPortID.Text = string.Empty;
            tbUpLimit.Text = string.Empty;
            tbValuePos.Text = string.Empty;
            tbXAxisName.Text = string.Empty;
            tbQueueSize.Text = string.Empty;
            tbYAxisName.Text = string.Empty;
            cbLowLimitColor.SelectedIndex = -1;
            cbUpLimitColor.SelectedIndex = -1;
            cbWaveColor.SelectedIndex = -1;
        }

        /// <summary>
        /// 点击ok按钮,触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            var tmpWaveColor = typeof(Color).GetProperty(cbWaveColor.Text);
            var tmpUpLimitColor = typeof(Color).GetProperty(cbUpLimitColor.Text);
            var tmpLowLimitColor = typeof(Color).GetProperty(cbLowLimitColor.Text);

            mainform.tempVisualizerConfig = new MainForm.VisualizerConfig()
            {
                Title = tbTitle.Text,
                WaveColor = (tmpWaveColor == null)? Color.Blue : (Color)tmpWaveColor.GetValue(null, null),
                UpLimit = int.Parse(tbUpLimit.Text),
                UpLimitColor = (tmpUpLimitColor == null)? Color.Red : (Color)tmpUpLimitColor.GetValue(null, null),
                LowLimit = int.Parse(tbLowLimit.Text),
                LowLimitColor = (tmpLowLimitColor == null) ? Color.Green : (Color)tmpLowLimitColor.GetValue(null, null),
                PortID = int.Parse(tbPortID.Text),
                FrameHead = tbFrameHead.Text,
                ValuePos = tbValuePos.Text,
                XAxisName = tbXAxisName.Text,
                QueueSize = int.Parse(tbQueueSize.Text),
                YAxisName = tbYAxisName.Text
            };
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 点击cancel按钮,触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            tbFrameHead.Text = string.Empty;
            tbLowLimit.Text = string.Empty;
            tbPortID.Text = string.Empty;
            tbUpLimit.Text = string.Empty;
            tbValuePos.Text = string.Empty;
            tbXAxisName.Text = string.Empty;
            tbQueueSize.Text = string.Empty;
            tbYAxisName.Text = string.Empty;
            cbLowLimitColor.SelectedIndex = -1;
            cbUpLimitColor.SelectedIndex = -1;
            cbWaveColor.SelectedIndex = -1;
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
